package ua.org.enginer.controls {
	
	import flash.display.Sprite;
	
	import mx.collections.ListCollectionView;
	import mx.controls.dataGridClasses.DataGridColumn;

	public class FilteredDataGrid extends HotKeysDataGrid {
		
		public var filterFields:Array /* of String */
		
		public function FilteredDataGrid() {
			super();
		}
		
		override public function set columns(value:Array):void {
			super.columns = value
			
			if (filterFields) return;
			
			filterFields = []
			for each(var column:DataGridColumn in value) {
				if (column.dataField) filterFields.push(column.dataField)
			}
		}
		
		override public function set dataProvider(value:Object):void {
			super.dataProvider = value
			if (filterByPrompt && value && value is ListCollectionView) {
				ListCollectionView(value).filterFunction = filterFunction
				ListCollectionView(value).refresh()
			}
		}
		
		
		private var _rowColorFunction:Function;
		public function set rowColorFunction(value:Function):void {
			_rowColorFunction = value;
		}

		public function get rowColorFunction():Function {
			return _rowColorFunction ? _rowColorFunction : defaultRowColorFunction
		}

		public function defaultRowColorFunction(item:Object):int {
			if (filterByPrompt) return -1;
			if (filterFunction(item)) 
				return 0xFFFF00
			else
				return -1
		}
		
		protected function defaultFilterFunction(item:Object):Boolean {
			for each(var field:String in filterFields) {
				if (item.hasOwnProperty(field) 
					&& String(item[field]).toLowerCase().indexOf(prompt.toLowerCase())>=0)
					return true
			}
			return false
		}
		
		private var _filterFunction:Function;
		public function set filterFunction(value:Function):void {
			_filterFunction = value;
		}

		public function get filterFunction():Function {
			return _filterFunction ? _filterFunction : defaultFilterFunction
		}


		
		private var _filterByPrompt:Boolean;
		public function set filterByPrompt(value:Boolean):void {
			_filterByPrompt = value;
			if (value && dataProvider && dataProvider is ListCollectionView) 
				ListCollectionView(dataProvider).filterFunction = filterFunction
			else
				ListCollectionView(dataProvider).filterFunction = null
				
			ListCollectionView(dataProvider).refresh()
		}
		
		[Bindable]
		public function get filterByPrompt():Boolean {
			return _filterByPrompt;
		}

		private var _prompt:String;
		public function set prompt(value:String):void {
			_prompt = value;
			invalidateList()
			if (filterByPrompt) ListCollectionView(dataProvider).refresh()
		}
		
		[Bindable]
		public function get prompt():String {
			return _prompt;
		}
		
		override protected function drawRowBackground(s:Sprite, rowIndex:int,
												y:Number, height:Number, color:uint, dataIndex:int):void
	    {
	    	if( this.rowColorFunction != null && dataProvider)
	    	{
	    		if( dataIndex < (this.dataProvider as ListCollectionView).length )
	    		{
		    		var item:Object = (this.dataProvider as ListCollectionView).getItemAt(dataIndex);
		    		//if (prompt) {
		    			var _color:int = rowColorFunction.call(this, item);
		    			if (_color>0)
		    				color = _color
		    		//}
	    		}
	    	}
	    	
	    	super.drawRowBackground(s, rowIndex, y, height, color, dataIndex);
	    }
	}
}

